package com.erhuogo.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erhuogo.bean.GridPageBean;
import com.erhuogo.dao.CityDao;
import com.erhuogo.dao.ProvinceDao;
import com.erhuogo.entity.City;
import com.erhuogo.entity.Province;
import com.erhuogo.enumerate.OpenStatus;
import com.erhuogo.service.CityService;
import com.erhuogo.service.ProvinceService;
import com.erhuogo.util.DateUtils;

/**
 * 省份Service的实现类
 * 
 * @author aibinxiao
 * @date 2016年10月26日 下午12:01:47
 */
public class ProvinceServiceImpl implements ProvinceService {
 	// 数据json格式转换
	private static ObjectMapper mapper = new ObjectMapper();
	
	// 日志
	private static Logger logger = LoggerFactory.getLogger(ProvinceServiceImpl.class);

	// 注入provinceDao
	private ProvinceDao provinceDao;

	public ProvinceDao getProvinceDao() {
		return provinceDao;
	}

	public void setProvinceDao(ProvinceDao provinceDao) {
		this.provinceDao = provinceDao;
	}
	
	// 注入CityDao
	private CityDao cityDao;

	public CityDao getCityDao() {
		return cityDao;
	}

	public void setCityDao(CityDao cityDao) {
		this.cityDao = cityDao;
	}
	
	// 注入CityService
	private CityService cityService;

	public CityService getCityService() {
		return cityService;
	}

	public void setCityService(CityService cityService) {
		this.cityService = cityService;
	}

	/**
	 * 1.获取所有的省份信息Service实现类
	 */
	@Override
	public List<Province> findAllProvince() {
		List<Province> listProvinces =  provinceDao.findAllProvince();
		return listProvinces;
	}

	/**
	 * 2.分页获取所有的省份信息Service实现类
	 */
	@Override
	public GridPageBean<Province> findByPage(String pageNo, String pageSize, String id, String name, String startDate,
			String endDate) {
		Integer pNo = 1;
		Integer pSize = 10;
		if(!StringUtils.isEmpty(pageNo) && !StringUtils.isEmpty(pageSize)){
			pNo = Integer.valueOf(pageNo);
			pSize = Integer.valueOf(pageSize);
		}
		int records = provinceDao.countByParams(id, name, startDate, endDate);
		GridPageBean<Province> result = new GridPageBean<Province>(pNo, records, pSize);
		//查询分页后的具体记录
		if(records <= result.getPageSize()){
			List<Province> rows = provinceDao.findPageByParams(id,name,startDate,endDate,result.getStartIndex(),records);
			result.setRows(rows);
		} else{
			List<Province> rows = provinceDao.findPageByParams(id,name,startDate,endDate,result.getStartIndex(),result.getPageSize());
			result.setRows(rows);
		}
		result.setPage(pNo);
		result.setRecords(records);
		result.setTotal(result.getTotal());
		return result;
	}

	/**
	 * 3.根据省份id获取省份信息Service实现类
	 */
	@Override
	public Province findById(String idStr) {
		Province province = null;
		if(!StringUtils.isEmpty(idStr)){
			Integer id = Integer.valueOf(idStr);
			province = provinceDao.findProvinceById(id);
		}
		return province;
	}

	/**
	 * 4.根据id保存或者更新省份信息Service实现类
	 */
	@Override
	public void save(String idStr, String name) throws Exception {
		Province province = new Province();
		// 如果id不为空则是修改更新
		if(!StringUtils.isEmpty(idStr)){
			Integer id = Integer.valueOf(idStr);
			province = provinceDao.findProvinceById(id);
		}else{
			// id为空则是添加新的三级分类
			province.setCreateTime(DateUtils.DateToStr(new Date()));
			province.setStatus(OpenStatus.OPEN.getStatusCode());
		}
		if(!StringUtils.isEmpty(name)){
			province.setName(name);
		}
		province.setAlterTime(DateUtils.DateToStr(new Date()));
		provinceDao.saveProvince(province);
	}

	/**
	 * 5.更新省份信息的启用状态Service实现类
	 */
	@Override
	public void updateStatus(String idStr, String statusStr) throws Exception {
		Integer status = null;
		Integer id = null;
		if(StringUtils.isEmpty(idStr)){
			throw new Exception("id参数不能为空");
		}else{
			id = Integer.valueOf(idStr);
		}
		if(StringUtils.isEmpty(statusStr)){
			throw new Exception("转换状态参数不能为空");
		}
		if(!StringUtils.isEmpty(statusStr)){
			status = Integer.valueOf(statusStr);
		}
		//开启
		if(OpenStatus.OPEN.getStatusCode()==status){
			provinceDao.updateStauts(id, OpenStatus.OPEN.getStatusCode());
			updateAllCityStatusByOneId(status,id);
		}
		//屏蔽
		if(OpenStatus.SHIELD.getStatusCode()==status){
			provinceDao.updateStauts(id, OpenStatus.SHIELD.getStatusCode());
			updateAllCityStatusByOneId(status,id);
		}
	}
	
	/**
	 * 6.根据一级分类ID更新所有的二级分类状态
	 * @param status
	 * @param id
	 * @throws Exception
	 */
	private void updateAllCityStatusByOneId(Integer status, Integer id) throws Exception {
		List<City> listCity = cityDao.findAllCityByProvinceId(id);
		if(listCity!=null && listCity.size()>0){
			logger.info("该省份下查询到["+listCity.size()+"]个城市,正在同步"+getTransStatus(status));
			for (City city : listCity) {
				cityService.updateStatus(city.getId().toString(), status.toString());
			}
		}else{
			logger.info("该省份下未查询到城市");
		}
	}
	
	/**
	 * 7.获取正在如何更新状态
	 * @param status
	 * @return
	 */
	public String getTransStatus(Integer status){
		String result = null;
		if(status!=null){
			if(status==0){
				result = "启用";
			}else{
				result = "屏蔽";
			}
		}
		return result;
	}

}
